<template>
	<view class="container">
		<view class="list-cell b-b m-t" @click="navigateTo('userinfo')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">个人资料</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<!-- <view class="list-cell b-b" @click="navigateTo('address')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">收货地址</text>
			<text class="cell-more yticon icon-you"></text>
		</view> -->
		<view class="list-cell m-t b-b" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit" @click="clearCache">清除缓存</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<!-- #ifdef APP-PLUS -->
		<view class="list-cell" v-if="platform=='android'"  @click="show()">
			<text class="cell-tit" >检查更新</text>
			<text class="cell-tip">当前版本 {{version}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell" v-else>
			<text class="cell-tit" >当前版本</text>
			<text class="cell-tip">{{version}}</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<!-- #endif -->
		<view class="list-cell log-out-btn"><text class="cell-tit" @click="logout">退出登录</text></view>
	</view>
</template>

<script>
import { isEmpty } from '../../utils';
import { mapMutations,mapState } from 'vuex';
import autoUpdater from '../../common/autoUpdater.min.js';
export default {
	data() {
		return {
			version:'',
			need_update:0,
			platform:'android'
		};
	},
	...mapState('user',['token','userinfo']),
	onLoad() {
		this.platform = uni.getSystemInfoSync().platform;
	},
	onShow() {
		//#ifdef APP-PLUS
		this.version= plus.runtime.version;
		this.checkVersion();
		//#endif
	},
	methods: {
		...mapMutations('user',['clearToken','setUserInfo']),
		checkVersion(){ //必须同步
			//#ifdef APP-PLUS
			this.version= plus.runtime.version;
			let params={
				v:this.version,
				t:new Date().getTime()
			}
			this.$ajax.get('common/compareversion',params).then(data=>{
				console.log(data);
				if(0==data.error){
					if(1==data.need_update){
						this.need_update=1;
						console.log(data.downurl);
						let note = isEmpty(data.note)? '更新内容:新版本'+data.version+',更新过程请不要重启和关闭APP' : data.note;
						autoUpdater.init({
							packageUrl: data.downurl,
							content: note,
							contentAlign: 'center',
							cancel: '取消',
							cancelColor: '#007fff',
							loadingColor:'#001327',
							windowHeight:200
						});
					}else{
						this.need_update=0;
					}
				}
			})
			// 更新应用资源  
			
			//#endif
		},
		show() {
			console.log(this.need_update);
			if(0==this.need_update){
				this.$api.msg('当前已经是最新版本');
			}else{
				autoUpdater.show();
			}
		},
		logout(){
			uni.showModal({
				content: '确定要退出么',
				success: (e)=>{
					if(e.confirm){
						this.clearToken()
						this.setUserInfo({});
						setTimeout(()=>{
							uni.switchTab({
								url: '/pages/index/index'
							});
						}, 1000)
					}
				}
			});	
		},
		clearCache(){
			try{
				uni.clearStorageSync();
				this.$api.msg('清除完成');
				setTimeout(function(){
					uni.switchTab({
						url: '/pages/index/index'
					});
				},800)
			}catch(e){
				//TODO handle the exception
			}
			
		}
	}
};
</script>

<style lang="scss">
page {
	background: $page-color-base;
}
.list-cell {
	display: flex;
	align-items: baseline;
	padding: 20upx $page-row-spacing;
	line-height: 60upx;
	position: relative;
	background: #fff;
	justify-content: center;
	&.log-out-btn {
		margin-top: 40upx;
		.cell-tit {
			color: $nav-bg;
			text-align: center;
			margin-right: 0;
		}
	}
	&.cell-hover {
		background: #fafafa;
	}
	&.b-b:after {
		left: 30upx;
	}
	&.m-t {
		margin-top: 16upx;
	}
	.cell-more {
		align-self: baseline;
		font-size: $font-lg;
		color: $font-color-light;
		margin-left: 10upx;
	}
	.cell-tit {
		flex: 1;
		font-size: $font-base + 2upx;
		color: $font-color-dark;
		margin-right: 10upx;
	}
	.cell-tip {
		font-size: $font-base;
		color: $font-color-light;
	}
	switch {
		transform: translateX(16upx) scale(0.84);
	}
}

</style>
